﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using  System.Data;
using  System.Data.Sql;

namespace BLL
{
   public  class InergalManage
   {
       #region 订单积分分摊
       /// <summary>
/// 客户订单积分分摊
/// </summary>
/// <param name="customerName">客户名称</param>
/// <param name="InergalType">积分类型</param>
/// <param name="InergalAmount">金额额</param>
        /// <param name="from">积分来源(订单号)</param>
        /// <param name="lists">启用层数提成信息</param>
/// <returns></returns>
       public List<string> InergalShare(string customerName, decimal InergalAmount, string from, List<Model.RecommendLevelSet> lists, Model.IntergalTypeSet IntergalTypeSetModel,Model.SYSPARAM sysparam,DateTime orderdate)
       {
           
           DateTime time = DateTime.Now;
           Model.V_VIPSET vipset = new Model.V_VIPSET();
           BLL.V_VIPSET bll_vipset = new V_VIPSET();
           vipset = bll_vipset.GetModel(customerName);
           List<string> list1 = new List<string>();
           //购造购物者插积分语句
           list1.Add("INSERT INTO IntegralWasteBook(IntergalId,IntergalType ,IntergalRange ,IntergalDateTime,IntergalExtendAttribute1,IntergalExtendAttribute3,IntergalExtendAttribute7,IntergalName) values ('" + Guid.NewGuid().ToString() + "'," + "'" + Convert.ToString((int)InergalTypeEnum.购物积分) + "'" + "," + InergalAmount + ",'" + time.ToString() + "'" + ",'" + customerName + "','" + InergalTypeEnum .购物积分.ToString()+ "'," + vipset.DQJF.ToString() + ",'" + from + "' )");
                      
           //客户积分等级
           BLL.V_CUSTOMER_AUTO V_CUSTOMER_AUTObll = new V_CUSTOMER_AUTO();
           
           Model.V_CUSTOMER_AUTO V_CUSTOMER_AUTOModel= V_CUSTOMER_AUTObll.GetModel(customerName);
           bool flag = true;//此积分是否能分摊标志

           #region 订单是否已过会员有效期判断
           if (sysparam.SP_PARAM13 == "1") //判断是否开启静态有效期
           {
               if (V_CUSTOMER_AUTOModel.BYZD14 != null)  //会员有静态有效期
               {
                   if (V_CUSTOMER_AUTOModel.BYZD14 != DateTime.MinValue && V_CUSTOMER_AUTOModel.BYZD14.ToString().Length > 1)
                   {
                       TimeSpan t = orderdate - V_CUSTOMER_AUTOModel.BYZD14;
                       if (t.Hours > 0)  //会员此订单已经过期
                       {
                           flag = false;
                       }
                   }
               }
           }
           #endregion

           #region 根据会员类型判断此单上级是否能拿提成
           if (sysparam.SP_PARAM14 == "1")  //判断系统是否启用会员类型,启用后要判断不同的类型是否能拿提成和自己的金额上级是否能拿提成
           {
               if (V_CUSTOMER_AUTOModel.BYZD9.Length > 0)  //有类型的
               {
                   switch (V_CUSTOMER_AUTOModel.BYZD9) //联盟会员,代销会员下单
                   {
                       case "002":  //联盟会员
                           flag = false;
                           break;
                       case "003":  //代销一级
                           flag = false;
                           break;
                       case "004":  //代销二级
                           flag = false;
                           break;
                       case "005":  //代销三级
                           flag = false;
                           break;
                   }
               }
           }
           #endregion
           if (flag)
           {
               #region 是否启用分摊级别,会员是否有推荐判断
               string CustomerLevel = V_CUSTOMER_AUTOModel.CustomerLevel;
               if (lists == null || CustomerLevel == null)
               {
                   flag = false;
               }
               #endregion
               
               if(flag)              
               {
                   string[] customerGroup = V_CUSTOMER_AUTOModel.CustomerLevel.Split('|');
                   
                   if (customerGroup.Length > 1)  //只有大于1时才会有上级
                   {
                       for (int j = 0; j < IntergalTypeSetModel.IntergalSetExtendAttribute6; j++) //循环启用级数
                       {
                           if (!flag)
                           {
                               flag = true;
                           }
                           Model.RecommendLevelSet rs = GetModel(lists, j + 1);
                           if (j < customerGroup.Length)
                           {
                               int len = customerGroup.Length;
                               if (len - j - 2 > -1)
                               {
                                   string upcode = customerGroup[len - j - 2];
                                   vipset = bll_vipset.GetModel(upcode);//得到上级会员积分信息
                                   decimal rate = 0; //分成比率
                                   #region 如果启用类型,则判断上级会员类型是否能拿到提成
                                   if (sysparam.SP_PARAM14 == "1")
                                   {
                                      Model.V_CUSTOMER_AUTO UpV_CUSTOMER_AUTOModel = V_CUSTOMER_AUTObll.GetModel(upcode);
                                      if (UpV_CUSTOMER_AUTOModel.BYZD9.Length > 0)
                                       {
                                           switch (UpV_CUSTOMER_AUTOModel.BYZD9)
                                           {
                                               case "000":
                                                   flag = false;
                                                   break;
                                               case "003":
                                                   flag = false;
                                                   break;
                                               case "004":
                                                   flag = false;
                                                   break;
                                               case "005":
                                                   flag = false;
                                                   break;
                                               case "001":
                                                   rate = rs.RecommendInergalSeparate;
                                                   break;
                                               case "002":
                                                   rate = rs.Attribute7;
                                                   if (j > 0) //联盟会员,只拿一级提成
                                                   {
                                                       flag = false;
                                                   }
                                                   break;
                                           }
                                       }
                                       
                                   }
                                   #endregion                                    
                                   #region 判断是否启用级别提成有效期
                                   if (sysparam.SP_PARAM16 == "1")
                                   {
                                       if (rs.Attribute5 > 0)
                                       {
                                           DateTime d = V_CUSTOMER_AUTOModel.CreatDateTime.AddMonths(rs.Attribute5);
                                           TimeSpan t = orderdate - d;
                                           if (t.Hours > 0) //大于0说明超出了会员级别有效期
                                           {
                                               flag = false;
                                           }
                                       }
                                       else
                                       {
                                           flag = false;
                                       }

                                   }
                                   #endregion
                                   if (flag)
                                   {
                                       if (rate > 0)
                                       {
                                           list1.Add("INSERT INTO IntegralWasteBook(IntergalId,IntergalType ,IntergalRange ,IntergalDateTime,IntergalExtendAttribute1,IntergalExtendAttribute3,IntergalExtendAttribute7,IntergalName) values ('" + Guid.NewGuid().ToString() + "'," + "'" + Convert.ToString((int)InergalTypeEnum.购物积分) + "'" + "," + InergalAmount * rate / 100 + ",'" + time.ToString() + "'" + ",'" + upcode + "','" + InergalTypeEnum.购物积分.ToString() + "'," + vipset.DQJF.ToString() + ",'" + from + "' )");
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           }
           return list1;
       }
       #endregion

       #region 积分枚举类型
       /// <summary>
/// 使用枚举类型作为积分类别
/// </summary>
       public  enum InergalTypeEnum
       {

           注册积分 = 0,//注册送积分
           购物积分 = 1,    //购物送积分
           登陆积分=2,
           完善资料=3,
           推荐积分=4,           //推荐送积分
           留言积分=5,
           评论积分=6
       }
       #endregion

       #region
       /// <summary>
       /// 得到上个月的积分
       /// </summary>
       /// <param name="CostomName"></param>
       /// <returns></returns>
       public DataSet getFirtMoth(string CostomName)
       { 
           string  sql =  "select   IntergalName ,first=sum(case   month(IntergalDateTime)   when  month(dateadd(m,-1,getdate()))   then   IntergalRange  else   0   end)  +"
                         +"  from      IntegralWasteBook where IntergalName = '" +  CostomName +   "'  group by  IntergalName   ";
           DataSet ds = DBUtility.SqlHelper.Query(DBUtility.SqlHelper.LocalSqlServer, sql);
           return ds;

       }
       #endregion

       #region 从RecommendLevelSet等级集合中拿出一条记录
       /// <summary>
       /// 从等级集合中拿出一条记录
       /// </summary>
       /// <param name="l"></param>
       /// <param name="i"></param>
       /// <returns></returns>
       public Model.RecommendLevelSet GetModel(List<Model.RecommendLevelSet> l,int i)
       {
           Model.RecommendLevelSet result = null;
           foreach (Model.RecommendLevelSet a in l)
           {
               if (a.RecommendManLevel == i)
               {
                   result = a;
               }
           }
           return result;
       }

       #endregion

       #region 注册送积分
       /// <summary>
       /// 注册人送注册积分
       /// </summary>
       /// <param name="customerName"></param>
       /// <param name="InergalAmount"></param>
       /// <param name="from"></param>
       /// <param name="intertype"></param>
       /// <param name="time"></param>
       /// <returns></returns>
       public List<string> RegInergalShare(string customerName, decimal InergalAmount, string from, InergalTypeEnum intertype, string time, Model.SYSPARAM sysparam, Model.IntergalTypeSet IntergalTypeSetModel)
       {
           List<string> list = null;
           Model.V_VIPSET vipset = new BLL.V_VIPSET().GetModel(customerName);
           if (vipset != null) //会员可以注册
           {
               list = new List<string>();
               list.Add("INSERT INTO IntegralWasteBook(IntergalId,IntergalType ,IntergalRange ,IntergalDateTime,IntergalExtendAttribute1,IntergalExtendAttribute3,IntergalExtendAttribute7,IntergalName) values ('" + Guid.NewGuid().ToString() + "'," + "'" + (int)intertype + "'" + "," + InergalAmount + ",'" + time + "'" + ",'" + customerName + "','" + intertype.ToString() + "'," + vipset.DQJF.ToString() + ",'" + from + "' )");               
           }
           return list;
       }
       /// <summary>
       /// 注册送推荐人推荐积分
       /// </summary>
       /// <param name="customerName"></param>
       /// <param name="InergalAmount"></param>
       /// <param name="from"></param>
       /// <param name="intertype"></param>
       /// <param name="time"></param>
       /// <param name="sysparam"></param>
       /// <param name="IntergalTypeSetModel"></param>
       /// <returns></returns>
       public List<string> RegReCommendInergalShare(string customerName, decimal InergalAmount, string from, InergalTypeEnum intertype, string time, Model.SYSPARAM sysparam, Model.IntergalTypeSet IntergalTypeSetModel)
       {
           List<string> list = null;
           Model.V_VIPSET vipset = new BLL.V_VIPSET().GetModel(customerName);           
           if (vipset != null) //推荐人存在
           {
               Model.V_CUSTOMER_AUTO customerauto = new BLL.V_CUSTOMER_AUTO().GetModel(customerName);
               if (customerauto != null)
               {
                   bool flag = true;
                   if (sysparam.SP_PARAM14 == "1")
                   {
                      if(customerauto.BYZD9 == "001" || customerauto.BYZD9 == "002")
                      {
                          flag=true;
                      }
                      else
                      {
                          flag=false;
                      }
                   }
                   if (flag)
                   {
                       list = new List<string>();
                       if (sysparam.SP_PARAM6 == 1) //推荐送积分开启
                       {
                           list.Add("INSERT INTO IntegralWasteBook(IntergalId,IntergalType ,IntergalRange ,IntergalDateTime,IntergalExtendAttribute1,IntergalExtendAttribute3,IntergalExtendAttribute7,IntergalName) values ('" + Guid.NewGuid().ToString() + "'," + "'" + (int)intertype + "'" + "," + InergalAmount + ",'" + time + "'" + ",'" + customerName + "','" + intertype.ToString() + "'," + vipset.DQJF.ToString() + ",'" + from + "' )");
                       }
                       string effactDate = "";//会员静态有效期 
                       if (sysparam.SP_PARAM13 == "1")  //会员静态有效期开启
                       {
                           if (IntergalTypeSetModel.IntergalSetEffact != DateTime.MinValue && IntergalTypeSetModel.IntergalSetEffact.ToString().Length > 0)
                           {
                               effactDate = ",BYZD14='" + IntergalTypeSetModel.IntergalSetEffact.ToString() + "'";
                           }
                       }
                       if (customerauto.CustomerLevel.Trim() != "")
                       {
                           list.Add("Update V_Customer_Auto set CustomerLevel='" + customerauto.CustomerLevel + "|" + from + "'" + effactDate + " where VC_CODE='" + from + "'");
                       }
                       else
                       {
                           list.Add("Update V_Customer_Auto set CustomerLevel='" + customerName + "|" + from + "'" + effactDate + " where VC_CODE='" + from + "'");
                       }
                   }
               }
           }
           return list;
       }
       #endregion

       #region 生成一条插入积分流水的Sql语句.用于无逻辑的地方
       /// <summary>
       /// 生成一条插入积分流水的Sql语句.用于无逻辑的地方
       /// </summary>
       /// <param name="customerName">加积分会员代码</param>
       /// <param name="InergalAmount">加积分值</param>
       /// <param name="from">积分来源</param>       
       /// <param name="intertype">积分类型</param>
       /// <param name="time">积分时间</param>
       /// <returns></returns>
       public List<string> InergalShare(string customerName, decimal InergalAmount, string from, InergalTypeEnum intertype,string time)
       {
           List<string> list = null;
           Model.V_VIPSET vipset=new BLL.V_VIPSET().GetModel(customerName);
           //Model.V_CUSTOMER_AUTO Customer_auto=null;
           //if ((int)intertype == 0)  //注册
           //{
           //    if (customerName != from)  //不相等是给推荐人加积分的,此时customerName为推荐人CODE，from为注册人CODE
           //    {
           //        Customer_auto = new BLL.V_CUSTOMER_AUTO().GetModel(customerName);  //如果是给推荐人加积分，取出推荐人扩展信息
           //    }
           //}
           if (vipset != null)  //会员存在
           {
               //Model.SYSPARAM sys = new BLL.SYSPARAM().GetModel("007");
               //Model.IntergalTypeSet intergalTypeSetMode = BLL.IntergalTypeSet.GetSysInterTypeSet();  //取基础设置
               //if (sys != null && intergalTypeSetMode != null)
               //{
               list = new List<string>();
               //    if (Customer_auto == null)  //注册人送积分
               //    {
                       list.Add("INSERT INTO IntegralWasteBook(IntergalId,IntergalType ,IntergalRange ,IntergalDateTime,IntergalExtendAttribute1,IntergalExtendAttribute3,IntergalExtendAttribute7,IntergalName) values ('" + Guid.NewGuid().ToString() + "'," + "'" + (int)intertype + "'" + "," + InergalAmount + ",'" + time + "'" + ",'" + customerName + "','" + intertype.ToString() + "'," + vipset.DQJF.ToString() + ",'" + from + "' )");
                       #region
                       //    }
               //   else         //推荐人送积分         
               //    {
               //        if (sys.SP_PARAM6 == 1) //推荐人送积分开启
               //        {
               //            list.Add("INSERT INTO IntegralWasteBook(IntergalId,IntergalType ,IntergalRange ,IntergalDateTime,IntergalExtendAttribute1,IntergalExtendAttribute3,IntergalExtendAttribute7,IntergalName) values ('" + Guid.NewGuid().ToString() + "'," + "'" + (int)intertype + "'" + "," + InergalAmount + ",'" + time + "'" + ",'" + customerName + "','" + intertype.ToString() + "'," + vipset.DQJF.ToString() + ",'" + from + "' )");
               //        }
               //        string effactDate = "";//会员静态有效期 
               //        if (sys.SP_PARAM13 == "1") //会员静态有效期开启
               //        {
               //            if (intergalTypeSetMode.IntergalSetEffact != DateTime.MinValue && intergalTypeSetMode.IntergalSetEffact.ToString().Length > 0)
               //            {
               //                effactDate = ",BYZD14='" + intergalTypeSetMode.IntergalSetEffact.ToString() + "'";
               //            }
               //        }
               //        if (Customer_auto.CustomerLevel.Trim() != "")
               //        {
               //            list.Add("Update V_Customer_Auto set CustomerLevel='" + Customer_auto.CustomerLevel + "|" + from + "'" + effactDate + " where VC_CODE='" + from + "'");
               //        }
               //        else
               //        {
               //            list.Add("Update V_Customer_Auto set CustomerLevel='" + customerName + "|" + from + "'" + effactDate + " where VC_CODE='" + from + "'");
               //        }
               //    }
                       //}
                       #endregion
                   }
           return list;
       }
       #endregion

       #region 订单现金分摊
       public List<string> CashShare(string customerName, decimal InergalAmount, string from, List<Model.RecommendLevelSet> lists, Model.IntergalTypeSet IntergalTypeSetModel,Model.SYSPARAM sysparam,string qishu,DateTime jisuanri,DateTime orderdate)
       {
           DateTime time = DateTime.Now;
           List<string> list1 = new List<string>();

           //使用分割线"|"
           BLL.V_CUSTOMER_AUTO V_CUSTOMER_AUTObll = new V_CUSTOMER_AUTO();
           Model.V_CUSTOMER_AUTO V_CUSTOMER_AUTOModel = V_CUSTOMER_AUTObll.GetModel(customerName);
           bool flag = true;//此金额是否能分摊标志
           if (V_CUSTOMER_AUTOModel != null)
           {
               #region 订单是否已过会员有效期判断
               if (sysparam.SP_PARAM13 == "1") //判断是否开启静态有效期
               {
                   if (V_CUSTOMER_AUTOModel.BYZD14 != null)  //会员有静态有效期
                   {
                       if (V_CUSTOMER_AUTOModel.BYZD14 != DateTime.MinValue && V_CUSTOMER_AUTOModel.BYZD14.ToString().Length > 1)
                       {
                           TimeSpan t = orderdate - V_CUSTOMER_AUTOModel.BYZD14;
                           if (t.Hours > 0)  //会员此订单已经过期
                           {
                               flag = false;
                           }
                       }
                   }
               }
               #endregion

               #region 根据会员类型判断此单上级是否能拿提成
               if (sysparam.SP_PARAM14 == "1")  //判断系统是否启用会员类型,启用后要判断不同的类型是否能拿提成和自己的金额上级是否能拿提成
               {
                   if (V_CUSTOMER_AUTOModel.BYZD9.Length > 0)  //有类型的
                   {
                       switch (V_CUSTOMER_AUTOModel.BYZD9) //联盟会员,代销会员下单
                       {
                           case "002":
                               flag = false;
                               break;
                           case "003":
                               flag = false;
                               break;
                           case "004":
                               flag = false;
                               break;
                           case "005":
                               flag = false;
                               break;
                       }
                   }
               }
               #endregion

               if (flag)
               {
                   #region 是否启用分摊级别,会员是否有推荐判断
                   string CustomerLevel = V_CUSTOMER_AUTOModel.CustomerLevel;
                   if (lists == null || CustomerLevel == null)
                   {
                       flag = false;
                   }
                   #endregion
                   if (flag)
                   {
                       string[] customerGroup = V_CUSTOMER_AUTOModel.CustomerLevel.Split('|');
                       if (customerGroup.Length > 1)  //只有大于1时才会有上级
                       {
                           for (int j = 0; j < IntergalTypeSetModel.IntergalSetExtendAttribute6; j++) //循环启用级数
                           {
                               if (!flag)
                               {
                                   flag = true;
                               }
                               Model.RecommendLevelSet rs = GetModel(lists, j + 1);
                               if (j < customerGroup.Length)
                               {
                                   int len = customerGroup.Length;
                                   if (len - j - 2 > -1)
                                   {
                                       string upcode = customerGroup[len - j - 2];
                                       decimal rate = 0; //分成比率
                                       #region 如果启用类型,则判断上级会员类型是否能拿到提成
                                       if (sysparam.SP_PARAM14 == "1")
                                       {
                                           Model.V_CUSTOMER_AUTO UpV_CUSTOMER_AUTOModel = V_CUSTOMER_AUTObll.GetModel(upcode);
                                           if (UpV_CUSTOMER_AUTOModel.BYZD9.Length > 0)
                                           {
                                               switch (UpV_CUSTOMER_AUTOModel.BYZD9)
                                               {
                                                   case "000":
                                                       flag = false;
                                                       break;
                                                   case "003":
                                                       flag = false;
                                                       break;
                                                   case "004":
                                                       flag = false;
                                                       break;
                                                   case "005":
                                                       flag = false;
                                                       break;
                                                   case "001":
                                                       rate = rs.RecommendCostSeparate;
                                                       break;
                                                   case "002":
                                                       rate = rs.Attribute8;
                                                       if (j > 0) //联盟会员,只拿一级提成
                                                       {
                                                           flag = false;
                                                       }
                                                       break;
                                               }
                                           }
                                       }
                                       #endregion
                                       #region 判断是否启用级别提成有效期
                                       if (sysparam.SP_PARAM16 == "1")
                                       {
                                           if (rs.Attribute5 > 0)
                                           {
                                               DateTime d = V_CUSTOMER_AUTOModel.CreatDateTime.AddMonths(rs.Attribute5);
                                               TimeSpan t = orderdate - d;
                                               if (t.Hours > 0) //大于0说明超出了会员级别有效期
                                               {
                                                   flag = false;
                                               }
                                           }
                                           else
                                           {
                                               flag = false;
                                           }

                                       }
                                       #endregion

                                       if (flag )
                                       {
                                           list1.Add("INSERT INTO BankRollDayToDayAccount (BDDA_QISHU, BDDA_USERCODE, BDDA_BankRoll, BDDA_ShareDate, BDDA_BillID) values ('" + qishu + "'," + "'" + upcode + "'" + "," + InergalAmount * rate / 100 + ",'" + time.ToString() + "','" + from + "' )");
                                       }
                                   }
                               }
                           }
                       }
                   }
               }
           }
           return list1;
       }
       #endregion
   }
}
